/**
 * 
 */
package org.t2.game.server.module.data.service.parser;

import java.util.List;

import org.gaming.tool.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t2.game.server.module.bag.model.Bag;
import org.t2.game.server.module.bag.service.BagService;
import org.t2.game.server.module.data.model.Data;
import org.t2.game.server.module.data.saver.PlayerTreasureSummonSaver;
import org.t2.game.server.module.data.saver.PlayerWeaponSummonSaver;
import org.t2.game.server.module.data.service.DataParserService;
import org.t2.game.server.module.data.struct.ShopData;

/**
 * 
 */
@Component
public class ShopParserService extends DataParserService {

	@Autowired
	private BagService bagService;
	@Autowired
	private PlayerWeaponSummonSaver playerWeaponSummonSaver;
	@Autowired
	private PlayerTreasureSummonSaver playerTreasureSummonSaver;
	
	@Override
	public String dataType() {
		return "shop";
	}

	@Override
	public void parser(long playerId, List<Data> datas, String reason) {
		Bag bag = bagService.getEntity(playerId);
		boolean needUpdate = false;
		for(Data data : datas) {
			ShopData shopData = GsonUtil.parseJson(data.getPlaintext(), ShopData.class);
			if(shopData.getCallData() != null) {
				
				if(shopData.getCallData().getEquipNum() != bag.getWeaponSummon()) {
					playerWeaponSummonSaver.setNumber(bag, reason, shopData.getCallData().getEquipNum());
					needUpdate = true;
				}
				
				if(shopData.getCallData().getTreasureNum() != bag.getTreasureSummon()) {
					playerTreasureSummonSaver.setNumber(bag, reason, shopData.getCallData().getTreasureNum());
					needUpdate = true;
				}
			}
		}
		if(needUpdate) {
			bagService.update(bag);
		}
		
	}

}
